///<reference path="../../../../node_modules/rxjs/Observable.d.ts"/>
import { Injectable } from '@angular/core';
import {Http,  Response,URLSearchParams} from "@angular/http";
import {Observable} from "rxjs/Observable";
import "rxjs/add/operator/map";
import "rxjs/add/operator/catch";
import "rxjs/add/observable/throw";
import  {KDMResponseModel,PowerColModel} from "../models/kdm-response-model"
import {environment} from "../../../environments/environment.local";
import {SetInfoModal} from "../models/set-info-modal"
import {AlarmReportModel} from '../models/alarm-report-model'
// import {isUndefined} from "util";
import {SetModalStatus} from '../models/set-modal-status-model'
import {BladeInfoModel} from '../models/blade-info-model'


@Injectable()
export class GetRTDataByKKSCodesService {
  private getByComApi: string="powerplant/component";
  private getState:string="powerplant/state";
  // private  kksApi:string="kdmService/rest/1.0/kksData?";
  private  kksApi:string="kdmService/rest/1.0/kksData?";
  private staticUrl="powerplant/";

  constructor(
    private http:Http,
  ) { }

  getBlade(kkscode:string,time:number):Observable<BladeInfoModel>{
    let api=this.staticUrl+'blade/one/'+kkscode+'/'+time
    // console.log(api)
    return new Observable<BladeInfoModel>(ob=>{
      this.http.get(api).map(this.extractData)
        .subscribe(data=>{
          ob.next(data);
          ob.complete()
        })
    })
  }

  getReport(method:string,id?:number):Observable<AlarmReportModel[]>{
    let api = this.staticUrl+'report'+'/'+method
    if(id||id===0)
      api+=('/'+id)
    return new Observable<AlarmReportModel[]>(
      ob=>{
        this.http.get(api).map(this.extractData)
          .subscribe(reports=>{
            ob.next(reports)
            ob.complete()
          })
      }
    )
  }

  getStdPowerById(id:number,time:number):Observable<any>{
    let api = this.getState+'/one/ss/'+id+'/'+time;
    return new Observable<any>(
      observe=>{
        this.http.get(api).map(this.extractData)
          .subscribe(data=>{
            observe.next(data);
            observe.complete()
          })
      }
    )
  }

  getPowerColum(id:number,time:number):Observable<PowerColModel[]>{
    //let api = this.getState+'/total/ss/'+id+'/'+time;
    let api = 'assets/data/total.json';
    return  new Observable<PowerColModel[]>(
      observe=>{
        this.http.get(api).map(this.extractData)
          .subscribe(data=>{
            observe.next(data);
            observe.complete();
          })
      }
    )
  }
  getAreaUnit(id:number,time:number):Observable<PowerColModel[]>{
    //let api = this.getState+'/total/ss/'+id+'/'+time;
    let api = 'assets/data/wujiang.json';
    return  new Observable<PowerColModel[]>(
      observe=>{
        this.http.get(api).map(this.extractData)
          .subscribe(data=>{
            observe.next(data);
            observe.complete();
          })
      }
    )
  }

  getAlarm(comName:string,kkscode:string,startTime?:number,endTime?:any):Observable<any[]>{
    let api=this.staticUrl+comName+'/'+kkscode
    if(startTime){
      api+='/'+startTime
    }
    if(endTime){
      api+='/'+endTime
    }
    return new Observable<any>(
      observe=>{
        this.http.get(api).map(this.extractData)
          .subscribe(data=>{
            observe.next(data);
            observe.complete()
          })
      }
    )
  }

  getDataByAA(comName:string,kksCode:string):Observable<any[]>{
    let api =this.staticUrl+comName+'/aa/'+kksCode;
    return new Observable<any>(
      observe=>{
        this.http.get(api).map(this.extractData)
          .subscribe(data=>{
            observe.next(data);
            observe.complete()
          })
      }
    )
  }

  getSetModalStatus(kkscode:string):Observable<SetModalStatus>{
    let api=this.getByComApi+'/diagnosis/'+kkscode
    return new Observable<SetModalStatus>(
      ob=>{
        this.http.get(api).map(this.extractData)
          .subscribe(status=>{
            ob.next(status);
            ob.complete();
          })
      }
    )
  }

  getDataByComName(comName:string,kkscode:string):Observable<any[]>{
    let api=this.getByComApi+'/'+comName+'/'+kkscode
    // console.log(api)
    return new Observable<any>(
      observe=>{
        this.http.get(api).map(this.extractData)
          .subscribe(data=>{
            observe.next(data);
            observe.complete()
          })
      }
    )
  }



  getAnemograph(target:string,kkscode:string,kkscode2?:string):Observable<any[]>{
    let api="powerplant/anemograph/"+target+'/aa/'+kkscode;
    if(kkscode2){
      api+='/'+kkscode2
    }
    return new Observable<any[]>(
      observe=>{
        this.http.get(api)
          .map(this.extractData)
          .subscribe(
            data=>{
              observe.next(data);
              observe.complete();
            }
          )
      }
    )
  }

  //获取状态
  getStationState(level:string,comName:string,id:number,time:any):Observable<any[]>{
    let api =this.getState+'/'+level+'/'+comName+'/'+id+'/'+time
    return new Observable<any[]>(
      observer=>{
        // console.log(api)
        this.http.get(api)
          .map(this.extractData)
          .subscribe(
            data=>{
              // let statusList=[]
              // data.forEach(e=>{
              //   statusList.push(e.state)
              // })
              observer.next(data);
              observer.complete();
            }
          )
      }
    )
  }

  //获取历史数据
  getKKSData(kksStr:string[],startTime:number,endTime:number):Observable<KDMResponseModel>{
    if(environment.local){
      return this.http.get('/assets/data/kksData_his.json').map(res => res.json())
    }
    let API=this.kksApi;
    let urlSearchParams: URLSearchParams;
    urlSearchParams = new URLSearchParams();
    urlSearchParams.append('kksCodes', kksStr.join(","));
    urlSearchParams.append('method', "getRTDataHistory");
    urlSearchParams.append('startTime', startTime+'');
    urlSearchParams.append('endTime', endTime+'');
    let body = urlSearchParams.toString();

    return this.http.post(API+body,{})
      .map(this.extractData)
      .catch(this.handleError);
  }

  getSetInfo(parentId:number,id:number,numTime:number):Observable<SetInfoModal>{
    let api=this.getByComApi+'/setinfo/'+parentId+'/'+id+'/'+numTime;
    return new Observable<any>(observe=>{
      this.http.get(api).map(this.extractData)
        .subscribe(data=>{
          observe.next(data);
          observe.complete()
        })
    })
  }

  getDataByComponent(comName:string,location:string,id:number,time?:string,extra?:string):Observable<any[]>{
    return new Observable<string[]>(observer=>{
      let api=this.getByComApi+'/'+comName
      if(location){
        api+='/'+location
      }
      api+='/'+id;
      if(time){
        api+=('/'+time)
      }
      if(extra){
        api+=('/'+extra)
      }
      this.http.get(api)
        .map(res=>res.json())
        .subscribe(data=>{
          observer.next(data);
          observer.complete();
        })
    })
  }

  getRestKksByComponent(comName:string,time?:string):Observable<string[]>{
    if(environment.local){
      return this.http.get('/assets/data/kksCodes.json').map(res => res.json())
    }

    return new Observable<string[]>(observer=>{
      let api=this.getByComApi+'/'+comName;
      if(time)
        api+=('/'+time);
      this.http.get(api)
        .map(res=>res.json())
        .subscribe(data=>{
          observer.next(data)
          observer.complete()
        })
    })
  }

  //获取实时数据
  getKKsDataSnap(kksStr:string[]):Observable<KDMResponseModel>{
    if(environment.local){
      return this.http.get('/assets/data/kksData_snap.json').map(res => res.json())
    }
    let API=this.kksApi;
    let urlSearchParams: URLSearchParams;
    urlSearchParams = new URLSearchParams();
    urlSearchParams.append('kksCodes', kksStr.join(","));
    urlSearchParams.append('method', "getRTDataSnapshot");
    let body = urlSearchParams.toString();

    return this.http.post(API+body,{})
      .map(this.extractData)
      .catch(this.handleError);
  }

  private extractData(res: Response) {
    let body = res.json();
    return body || {};
  }

  private handleError(error:Response|any) {
    let errmsg :string;
    if(error instanceof Response){
      const body = error.json()||{};
      const err = body.error || JSON.stringify(body);
      errmsg=`${error.status}--${error.statusText||''}${err}`;
    }
    else{
      errmsg=error.message?error.message:error.toString();
    }
    console.log(errmsg);
    return Observable.throw(errmsg);
  }
}
